<?php
// +----------------------------------------------------------------------
// | [ api-yun.lib ] wo.pan
// +----------------------------------------------------------------------
// | Author: 北菜菜 <1440802@qq.com>
// +----------------------------------------------------------------------

namespace wopan\lib;

/**
 * Class WoPanAES
 * @package wopan\lib
 */
class WoPanAES
{
    /**
     *
     * @param string $string 需要加密的字符串
     * @param string $key 密钥
     * @return string
     */
    public static function encrypt($string, $key)
    {
        // 如果不是字符串，自动转换为字符串
        if (!is_string($string)) $string = json_encode($string);
        // openssl_encrypt 加密不同Mcrypt，对秘钥长度要求，超出16加密结果不变
        $data = openssl_encrypt($string, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, self::getiv());
        return base64_encode($data);
    }
    /**
     * @param string $string 需要解密的字符串
     * @param string $key 密钥
     * @return string
     */
    public static function decrypt($string, $key)
    {
        return openssl_decrypt(base64_decode($string), 'AES-128-CBC', $key, OPENSSL_RAW_DATA, self::getiv());
    }

    public static function getiv()
    {
        return 'wNSOYIB1k1DjY5lA';
    }
}
